<template>
	<el-container>
		<el-aside width="350px" v-loading="showDictLoading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="dictFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="dict" class="menu" node-key="id" :data="dictList" :props="dictProps" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="dictFilterNode" @node-click="dictClick">
						<template #default="{node, data}">
							<span class="custom-tree-node">
								<span class="label">{{ node.label }}</span>
								<span class="code">{{ data.dictCode }}</span>
								<span class="do">
									<el-button-group>
										<el-button v-auth="'system:dict:edit'" type="primary" size="small" icon="el-icon-edit" @click.stop="editDict(data)"></el-button>
										<el-button v-auth="'system:dict:remove'" type="danger" size="small" icon="el-icon-delete" @click.stop="delDict(node, data)"></el-button>
									</el-button-group>
								</span>
							</span>
						</template>
					</el-tree>
				</el-main>
				<el-footer style="height:51px;">
					<el-button v-auth="'system:dict:add'" type="primary" size="small" icon="el-icon-plus" style="width: 100%;" @click="addDict">
						字典类型
					</el-button>
				</el-footer>
			</el-container>
		</el-aside>
		<el-container class="is-vertical">
			<el-header>
				<div class="left-panel">
					<el-button v-auth="'system:dictData:add'" type="primary" icon="el-icon-plus" @click="add">新增</el-button>
					<el-button v-auth="'system:dictData:remove'" type="danger" plain icon="el-icon-delete" :disabled="selection.length===0" @click="batchDel">删除</el-button>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="dictDataList" row-key="id" :params="dictDataParams" @selection-change="selectionChange" stripe>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="名称" prop="dictItem" align="center" width="150"></el-table-column>
					<el-table-column label="键值" prop="dictValue" align="center" width="240"></el-table-column>
					<el-table-column label="排序号" prop="orderNum" align="center" width="150"></el-table-column>
					<el-table-column label="状态" prop="status" align="center" width="150">
						<template #default="scope">
							<el-switch v-model="scope.row.status" @change="changeStatus($event, scope.row)" :loading="scope.row.$switch_status" active-value="1" inactive-value="0"></el-switch>
						</template>
					</el-table-column>
					<el-table-column label="备注" prop="remark" align="center" min-width="150"></el-table-column>
					<el-table-column label="操作" fixed="right" align="center" width="150">
						<template #default="scope">
							<el-button-group>
								<el-button v-auth="'system:dictData:edit'" text type="primary" size="small" @click="tableEdit(scope.row, scope.$index)">编辑</el-button>
								<el-popconfirm title="确定删除吗？" @confirm="tableDel(scope.row, scope.$index)">
									<template #reference>
										<el-button v-auth="'system:dictData:remove'" text type="primary" size="small">删除</el-button>
									</template>
								</el-popconfirm>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>

	<dict-dialog v-if="dialog.dict" ref="dictDialog" @success="handleDictSuccess" @closed="dialog.dict=false"></dict-dialog>

	<dict-data-dialog v-if="dialog.dictData" ref="dictDataDialog" @success="handleDictDataSuccess" @closed="dialog.dictData=false"></dict-data-dialog>

</template>

<script>
import dictDialog from './dict.vue'
import dictDataDialog from './dictData.vue'

export default {
	name: 'dict',
	components: {
		dictDialog,
		dictDataDialog
	},
	data() {
		return {
			dialog: {
				dict: false,
				dictData: false
			},
			showDictLoading: true,
			dictList: [],
			dictFilterText: '',
			dictProps: {
				label: 'dictName'
			},
			selection: [],
			dictDataParams: {
				dictTypeId: null
			},
			dictDataList: []
		}
	},
	watch: {
		dictFilterText(val) {
			this.$refs.dict.filter(val);
		}
	},
	mounted() {
		this.getDict();
	},
	methods: {
		// 加载字典数据
		async getDict() {
			this.showDictLoading = false;
			this.dictList = await this.$API.system.dict.list.get();

			// 获取第一个节点,设置选中 & 加载明细列表
			const firstNode = this.dictList[0];
			if (firstNode) {
				this.$nextTick(() => {
					this.$refs.dict.setCurrentKey(firstNode.id);
				})
				this.dictDataParams.dictTypeId = firstNode.id;
				this.dictDataList = this.$API.system.dictData.page;
			}
		},
		// 字典过滤
		dictFilterNode(value, data) {
			if (!value) return true;
			const targetText = data.dictName + data.dictCode;
			return targetText.indexOf(value) !== -1;
		},
		// 字典点击
		dictClick(data) {
			this.$refs.table.upData({dictTypeId: data.id});
		},
		// 新增字典
		addDict() {
			this.dialog.dict = true;
			this.$nextTick(() => {
				this.$refs.dictDialog.open();
			})
		},
		// 编辑字典
		editDict(data) {
			this.dialog.dict = true;
			this.$nextTick(() => {
				this.$refs.dictDialog.open('edit').setData(data);
			})
		},
		// 删除字典
		delDict(node, data) {
			this.$confirm(`确定删除选中的 ${data.dictName} 项数据吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.showDictLoading = true;
				const checkId = this.$refs.dict.getCurrentKey();
				const reqData = {idList: [data.id]};
				this.$API.system.dict.remove.delete(reqData).then(() => {
					this.$message.success("删除成功");

					// 重新加载字典及字典项
					this.$API.system.dict.list.get().then(res => {
						this.dictList = res;
						if (data.id === checkId) {
							const firstNode = this.dictList[0];
							if (firstNode) {
								this.$nextTick(() => {
									this.$refs.dict.setCurrentKey(firstNode.id);
								})
								this.dictDataParams.dictTypeId = firstNode.id;
								this.$refs.table.refresh();
							}
						} else {
							this.$nextTick(() => {
								this.$refs.dict.setCurrentKey(checkId);
							})
						}
					});
				})
				this.showDictLoading = false;
			})
		},
		// 字典项新增
		add() {
			this.dialog.dictData = true;
			this.$nextTick(() => {
				this.$refs.dictDataDialog.open().setData({dictTypeId: this.dictDataParams.dictTypeId});
			})
		},
		// 字典项批量删除
		batchDel() {
			this.$confirm(`确定删除选中的 ${this.selection.length} 项数据吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				const loading = this.$loading();
				const idList = [];
				this.selection.forEach(e => idList.push(e.id));
				const reqData = {idList: idList};
				this.$API.system.dictData.remove.delete(reqData).then(() => {
					this.$refs.table.refresh();
					this.$message.success("删除成功");
				})
				loading.close();
			})
		},
		// 编辑字典项
		tableEdit(row) {
			this.dialog.dictData = true;
			this.$nextTick(() => {
				this.$refs.dictDataDialog.open('edit').setData(row);
			})
		},
		// 删除字典项
		tableDel(row) {
			const reqData = {idList: [row.id]};
			this.$API.system.dictData.remove.delete(reqData).then(() => {
				this.$refs.table.refresh();
				this.$message.success("删除成功");
			})
		},
		// 修改状态
		changeStatus(val, row) {
			row.$switch_status = true;
			const reqData = {id: row.id, status: row.status};
			this.$API.system.dictData.changeStatus.put(reqData).then(() => {
				delete row.$switch_status;
				this.$refs.table.refresh();
				this.$message.success("操作成功");
			})
		},
		// 表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		// 更新字典数据
		handleDictSuccess() {
			this.showDictLoading = false;
			const checkId = this.$refs.dict.getCurrentKey();
			this.$API.system.dict.list.get().then(data => {
				this.dictList = data;

				this.$nextTick(() => {
					this.$refs.dict.setCurrentKey(checkId);
				})
			});
		},
		// 更新字典项数据
		handleDictDataSuccess() {
			this.$refs.table.refresh();
		}
	}
}
</script>

<style scoped>
.menu:deep(.el-tree-node__label) {
	display: flex;
	flex: 1;
	height: 100%;
}

.custom-tree-node {
	display: flex;
	flex: 1;
	align-items: center;
	justify-content: space-between;
	font-size: 14px;
	padding-right: 24px;
	height: 100%;
}

.custom-tree-node .code {
	font-size: 12px;
	color: #999;
}

.custom-tree-node .do {
	display: none;
}

.custom-tree-node:hover .do {
	display: inline-block;
}
</style>
